//
//  ViewController.m
//  适配之道
//
//  Created by macbook on 2017/5/18.
//  Copyright © 2017年 oceansoft. All rights reserved.
//

#import "ViewController.h"
#import "CustomLayout.h"
#import "AFNetWorking.h"

#import "FirstPartCollectionViewCell.h"


@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    CGFloat spaceLine;
}

@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain",nil];
    
    [manager GET:@"http://localhost:8888/02.php" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    
    NSArray *filterArray = @[@"ab", @"abc",@"BBB"];
    NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(SELF IN %@)",filterArray];
    
    NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
    
    
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"NOT(SELF IN %@)",array];
    
    NSLog(@"%@", [filterArray filteredArrayUsingPredicate:predicate1]);
    
    
    
    //1/[UIScreen mainScreen].scale
    
    spaceLine = KLineWidth;
    
    CustomLayout *layout = [[CustomLayout alloc]init];
    
    layout.minimumLineSpacing = spaceLine;
    
    layout.minimumInteritemSpacing = spaceLine;
    
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _mainCollectionView.collectionViewLayout = layout;
    
    [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"kcollectionview"];
    
    [_mainCollectionView registerNib:[UINib nibWithNibName:KFirstPartCollectionViewCell  bundle:nil] forCellWithReuseIdentifier:KFirstPartCollectionViewCell];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        FirstPartCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KFirstPartCollectionViewCell forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor redColor];
        
        return cell;
    }
    else{
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kcollectionview" forIndexPath:indexPath];
        
        //(red: 237/255.0, green: 73/255.0, blue: 86/255.0, alpha: 1)
        cell.backgroundColor = [UIColor colorWithRed:237/255.0 green:73/255.0 blue:86/255.0 alpha:1];
        
        //    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_home_category_left"]];
        
        //    cell.layer.borderColor=[UIColor darkGrayColor].CGColor;
        //    cell.layer.borderWidth=0.3;
        
        return cell;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 120;
    
    if (indexPath.section == 0) {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*240 / 750);
    }
    else if (indexPath.section == 1) {
        if(indexPath.item < 2){
            
            CGFloat width = ([UIScreen mainScreen].bounds.size.width - spaceLine * 2) / 2;
            
            if ([UIScreen mainScreen].bounds.size.width > 320) {
                
            }else{
                CGFloat scale = [UIScreen mainScreen].bounds.size.width / 375 ;
                
                height = height *scale;
            }
            
            return CGSizeMake(width, height);
        }
        else{
            CGFloat width = ([UIScreen mainScreen].bounds.size.width - spaceLine * 3) / 4;
            
            if ([UIScreen mainScreen].bounds.size.width > 320) {
                
            }else{
                CGFloat scale = [UIScreen mainScreen].bounds.size.width / 375 ;
                
                height = height *scale;
            }
            
            return CGSizeMake(width, height);
        }
    }
    else{
        
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - spaceLine * 2) / 3;

        return CGSizeMake(width, height);
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if(section == 0){
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, 0);
    }
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
}

- (UIColor *)getRandomColor
{
    int R = (arc4random() % 256) ;
    int G = (arc4random() % 256) ;
    int B = (arc4random() % 256) ;
    UIColor *color = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];
    
    return color;
}

-(void)viewWillLayoutSubviews
{
    [self.mainCollectionView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
